<?php

/*
 * Class that operate on table 'formulario'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2009-08-25 19:00
 */
class FormularioDAO {

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return FormularioMySql
	 */
	public function load($id){
		$sql = 'SELECT * FROM formulario WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM formulario';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM formulario ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	/**
 	 * Delete record from table
 	 * @param formulario primary key
 	 */
	public function delete($codFormulario){
		$sql = 'DELETE FROM formulario WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		return $this->executeUpdate($sqlQuery);
	}

 	/**
	* Delete record from table
	* @param campo primary key
	*/
	public function deleteByCampo($codCampo)
	{
		$sql = 'DELETE FROM formulariocampo WHERE codCampo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codCampo);
	 	return $this->execute($sqlQuery);
 	}

	/**
 	 * Insert record to table
 	 *
 	 * @param FormularioMySql formulario
 	 */
	public function insert($formulario){
		$sql = 'INSERT INTO formulario (nomeModelo, codCurso, tipoEdital, statusFormulario, dataCriacao, dataAlteracao, htmlFormulario) VALUES (?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);

		$sqlQuery->set($formulario->nomeModelo);
		$sqlQuery->set($formulario->codCurso);
		$sqlQuery->set($formulario->tipoEdital);
		$sqlQuery->set($formulario->statusFormulario);
		$sqlQuery->set($formulario->dataCriacao);
		$sqlQuery->set($formulario->dataAlteracao);
		$sqlQuery->set($formulario->htmlFormulario);

		$id = $this->executeInsert($sqlQuery);
		$formulario->codFormulario = $id;
		return $id;
	}

	/**
 	 * Update record in table
 	 *
 	 * @param FormularioMySql formulario
 	 */
	public function update($formulario){
		$sql = 'UPDATE formulario SET nomeModelo = ?, codCurso = ?, tipoEdital = ?, statusFormulario = ?, dataAlteracao = ?, htmlFormulario = ? WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);

		$sqlQuery->set($formulario->nomeModelo);
		$sqlQuery->set($formulario->codCurso);
		$sqlQuery->set($formulario->tipoEdital);
		$sqlQuery->set($formulario->statusFormulario);
		$sqlQuery->set($formulario->dataAlteracao);
		$sqlQuery->set($formulario->htmlFormulario);
		$sqlQuery->setNumber($formulario->codFormulario);

		return $this->executeUpdate($sqlQuery);
	}

	/**
	 * Read row
	 *
	 * @return FormularioMySql
	 */
	protected function readRow($row) {
		$formulario = new Formulario();

		$formulario->codFormulario = $row['codFormulario'];
		$formulario->nomeModelo = $row['nomeModelo'];
		$formulario->codCurso = $row['codCurso'];
		$formulario->tipoEdital = $row['tipoEdital'];
		$formulario->statusFormulario = $row['statusFormulario'];
		$formulario->dataCriacao = $row['dataCriacao'];
		$formulario->dataAlteracao = $row['dataAlteracao'];
		$formulario->htmlFormulario = $row['htmlFormulario'];

		return $formulario;
	}

	protected function getList($sqlQuery) {
		$tab = QueryExecutor :: execute($sqlQuery);
		$ret = array ();
		for ($i = 0; $i < count($tab); $i++) {
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}

	/**
	 * Get row
	 *
	 * @return FormularioMySql
	 */
	protected function getRow($sqlQuery) {
		$tab = QueryExecutor :: execute($sqlQuery);
		return $this->readRow($tab[0]);
	}

	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery) {
		return QueryExecutor :: execute($sqlQuery);
	}

	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery) {
		return QueryExecutor :: executeUpdate($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery) {
		return QueryExecutor :: executeInsert($sqlQuery);
	}

	/**
	 * listarFormularioPorCurso
	 * @param int codCurso
	 */
	public function listarFormularioPorCurso($codCurso) {
		$sql = 'SELECT codFormulario, codCurso, dataCriacao, dataAlteracao, nomeModelo, tipoEdital, statusFormulario FROM formulario WHERE codCurso = ? ORDER BY dataCriacao DESC';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($codCurso);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulario = new Formulario();
			$formulario->codFormulario = $tab[$i]["codFormulario"];
			$formulario->codCurso = $tab[$i]["codCurso"];
			$formulario->dataCriacao = $tab[$i]["dataCriacao"];
			$formulario->dataAlteracao = $tab[$i]["dataAlteracao"];
			$formulario->nomeModelo = $tab[$i]["nomeModelo"];
			$formulario->tipoEdital = $tab[$i]["tipoEdital"];
			$formulario->statusFormulario = $tab[$i]["statusFormulario"];
			$ret[$i] = $formulario;
		}
		return $ret;
	}

	/**
	 * listarFormulariosPorCoordenador
	 * @param int codCoordenador
	 */
	public function listarFormulariosPorCoordenador ($codCoordenador){
		$sql = 'SELECT f.codFormulario, f.codCurso, f.dataCriacao, f.dataAlteracao, f.nomeModelo, f.tipoEdital, f.statusFormulario
				FROM formulario AS f
				INNER JOIN curso AS c ON (c.codCurso = f.codCurso)
				WHERE c.codCoordenador = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($codCoordenador);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulario = new Formulario();
			$formulario->codFormulario = $tab[$i]["codFormulario"];
			$formulario->codCurso = $tab[$i]["codCurso"];
			$formulario->dataCriacao = $tab[$i]["dataCriacao"];
			$formulario->dataAlteracao = $tab[$i]["dataAlteracao"];
			$formulario->nomeModelo = $tab[$i]["nomeModelo"];
			$formulario->tipoEdital = $tab[$i]["tipoEdital"];
			$formulario->statusFormulario = $tab[$i]["statusFormulario"];
			$ret[$i] = $formulario;
		}
		return $ret;
	}

	/**
	 * obterHtmlFormulario
	 * @param int codFormulario
	 */
	public function obterHtmlFormulario($codFormulario){
		$sql = 'SELECT htmlFormulario FROM formulario WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulario = new Formulario();
			$formulario->htmlFormulario = stripslashes($tab[$i]["htmlFormulario"]);
			$ret[$i] = $formulario;
		}
		return $ret;
	}

	/**
	 * Metodo para obter nome do formulario por codFormulario
	 * @param int codFormulario
	 */
	public function obterNomeFormulario($codFormulario) {
	$sql = 'SELECT nomeModelo FROM formulario WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulario = new Formulario();
			$formulario->nomeModelo = $tab[$i]["nomeModelo"];
			$ret[$i] = $formulario;
		}
		return $ret;
	}

	/**
	 * Metodo para obter nome e curso do formulario
	 * @param int codFormulario
	 */
	public function obterNomeCursoFormulario($codFormulario) {
		$sql = 'SELECT f.nomeModelo, c.nomeCurso, c.codCurso ' .
			'FROM formulario f JOIN curso c ' .
			'WHERE f.codCurso = c.codCurso ' .
			'AND f.codFormulario = ? ';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulario = new Formulario();
			$formulario->nomeModelo = $tab[$i]["nomeModelo"];
			$formulario->nomeCurso = $tab[$i]["nomeCurso"];
			$formulario->codCurso = $tab[$i]["codCurso"];
			$ret[$i] = $formulario;
		}
		return $ret;
	}

	/**
	 * Metodo que retorna o status de um formulario
	 * @param int codFormulario
	 */
	public function obterStatusFormulario($codFormulario) {
		$sql = 'SELECT statusFormulario FROM formulario WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulario = new Formulario();
			$formulario->statusFormulario = $tab[$i]["statusFormulario"];
			$ret[$i] = $formulario;
		}
		return $ret;
	}

	/**
	 * Metodo para alterar o status de um formulario
	 * @param int codFormulario
	 * @param int statusFormulario
	 */
	public function mudarStatusFormulario($codFormulario, $statusFormulario) {
		$sql = 'UPDATE formulario SET statusFormulario = ? WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);

		$sqlQuery->set($statusFormulario);
		$sqlQuery->setNumber($codFormulario);

		return $this->executeUpdate($sqlQuery);
	}

	/**
	 * Metodo que retorna o tipo de edital de um formulario
	 * @param int codFormulario
	 */
	public function obterTipoEditalFormulario($codFormulario) {
		$sql = 'SELECT tipoEdital FROM formulario WHERE codFormulario = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codFormulario);
		$tab = $this->execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$formulario = new Formulario();
			$formulario->tipoEdital = $tab[$i]["tipoEdital"];
			$ret[$i] = $formulario;
		}
		return $ret;
	}

}
?>